<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Goods as GoodsM;
use app\common\model\LogType;
use app\common\model\TradeOrder;
use think\Cache;
use think\Db;
use think\Request;

/**
 * 交易接口
 */
class Bill extends Api
{
    /**
     * 背包列表
     */
    public function bag_list()
    {
        $goods_id = input('goods_id', 0, 'intval');
        $list = GoodsM::goods_list($goods_id);
        foreach ($list as &$item) {
            $item['num'] = Db::name('goods_order')->where(['uid' => $this->auth->id, 'goods_id' => $item['id']])->value('num') ?: 0;
        }

        $this->success('返回成功', $list);
    }

    /**
     * 赠送
    */
    public function give()
    {
        $to_user = input('to_user', '', 'trim');
        $num = input('num', 0, 'intval');
        if ($num <= 0) {
            $this->error('赠送数量错误');
        } else {
            $to_uid = model('user')->where(['username' => $to_user])->value('id');
            if ($to_uid) {
                $goods_id = input('goods_id', 0, 'intval');
                $res = \app\common\model\Bill::give($this->auth->id, $to_uid, $goods_id, $num);
                if ($res['error'] > 0) {
                    $this->error($res['msg']);
                }
            } else {
                $this->error('您所赠送的用户不存在');
            }
        }

        $this->success('赠送成功');
    }

    /**
     * 挂卖
     */
    public function sell()
    {
        $price = input('price', 0, 'floatval');
        $num = input('num', 0, 'intval');
        $goods_id = input('goods_id', 0, 'intval');
        if ($num <= 0) {
            $this->error('挂卖数量错误');
        } elseif ($goods_id <= 0) {
            $this->error('挂卖宠物错误');
        } else {
            $res = \app\common\model\Bill::sell($this->auth->id, $goods_id, $num, $price);
            if ($res['error'] > 0) {
                $this->error($res['msg']);
            }
        }

        $this->success('挂卖成功');
    }

    /**
     * 挂卖列表
    */
    public function sell_list(Request $request)
    {
        $page = $request->param('page',1);
        $rows = $request->param('rows',20);
        $where = ['status' => 0];

        $items = TradeOrder::where($where)
            ->page($page,$rows)
            ->order('id', 'desc')
            ->select();
        $items2 = [];
        foreach ($items as $item) {
            $goods_order = Db::name('goods_order')->where(['id' => $item['tradeid']])->find();
            $items2[] = [
                'pets_name' => $goods_order['name'],
                'pets_logo' => $goods_order['image'],
                'pets_price' => $item['amount'] / $item['num'],
                'pets_num' => $item['num'],
                'id' => $item['id']
            ];
        }
        $total  = TradeOrder::where($where)->count();
        $totalPage = ceil($total/$rows);
        $data = [
            'total' =>$total,
            'items' => $items2,
            'totalPage' => $totalPage
        ];
        $this->success('返回成功', $data);
    }

    /**
     * 购买
    */
    public function buy_pets()
    {
        $id = input('id', 0, 'intval');
        $num = input('num', 1, 'intval');
        $res = \app\common\model\Bill::buy_pets($this->auth->id, $id, $num);
        if ($res['error'] > 0) {
            $this->error($res['msg']);
        } else {
            $this->success('购买成功', $res['data']);
        }
    }

    /**
     * 挂卖购买交易记录
    */
    public function sell_buy_log(Request $request)
    {
        $type = input('type', 0, 'intval');
        $page = $request->param('page',1);
        $rows = $request->param('rows',20);
        $coin_id = $request->param('coin_id',config('usdt_id'));
        $where = ['uid' => $this->auth->id, 'coin_id' => $coin_id, 'type' => LogType::get_type_id('sell_pets')];
        if ($type == 1) {
            $where['to_uid'] = $this->auth->id;
            unset($where['uid']);
        }

        $items = TradeOrder::where($where)
            ->page($page,$rows)
            ->order('id', 'desc')
            ->select();
        $items2 = [];
        foreach ($items as $item) {
            $status_str = '';
            foreach (TradeOrder::$buy_sell_status as $key => $status) {
                if ($key == $item['status']) {
                    $status_str = $status;
                }
            }
            $pets_name = Db::name('goods_order')->where(['id' => $item['tradeid']])->value('name');
            $items2[] = [
                'id' => $item['id'],
                'name' => $pets_name,
                'num' => $item['num'],
                'amount' => $item['amount'],
                'status_str' => $status_str,
                'createtime' => date("Y-m-d H:i:s", $item['createtime'])
            ];
        }
        $total  = TradeOrder::where($where)->count();
        $totalPage = ceil($total/$rows);
        $data = [
            'total' =>$total,
            'items' => $items2,
            'totalPage' => $totalPage
        ];
        $this->success('返回成功', $data);
    }
}
